package us.versus.them.hmap;

class TextHash2Ppm {

#if neko

	static var STDOUT = neko.io.File.stdout();
	
	public static function main() {
		var inn:String = null;
		var out:String = null;
		var args = neko.Sys.args();
		var i = 0;
		while ( i < args.length ) {
			var arg = args[ i ];
			switch ( arg ) {
				case '-i':
					inn = args[ ++i ];
				case '-o':
					out = args[ ++i ];
				default:
					inn = out = null;
					break;
			}
			i++;
		}
		if ( null == inn || null == out ) {
			STDOUT.writeString( '\nusage: [neko] textHash2Ppm -i infile -o outfile\n\n' );
		} else {
			STDOUT.writeString( 'read from ' + inn + ' and write to ' + out + '\n' );
			pout( 
				neko.io.File.read( inn, false )
				, neko.io.File.write( out, true )
			);
		}
	}

	public static function pout( inn, out ) {
		var lines = new Array< String >();
		try {
			var line:String;
			while ( null != ( line = inn.readLine() ) ) {
				lines.push( line );
			}
		} catch ( e:Dynamic ){
			// eof
		}

		// ppm header is nice and ez!
		out.writeString( 'P6\n' );
		out.writeString( lines[ 0 ].length + ' ' + lines.length + '\n' );
		out.writeString( '255\n' );

		for ( line in lines ) {
			for ( i in 0 ... line.length ) {
				var char = line.charAt( i ) ;
				var value = if ( '#' == char ) 255; else 0;
				out.writeByte( value );
				out.writeByte( value );
				out.writeByte( value );
			}
		}

	}

#end
}
